
#include <iostream>
#include <memory>
#include <string>

// 策略接口
class PaymentStrategy {
public:
    virtual ~PaymentStrategy() = default;
    virtual void pay(int amount) const = 0; // 支付接口
};

// 具体策略：现金支付
class CashPayment : public PaymentStrategy {
public:
    void pay(int amount) const override {
            std::cout << "Paid $" << amount << " using cash.\n";
    }
};

// 具体策略：信用卡支付
class CreditCardPayment : public PaymentStrategy {
private:
    std::string cardNumber;

public:
    explicit CreditCardPayment(const std::string& cardNum) : cardNumber(cardNum) {}

    void pay(int amount) const override {
            std::cout << "Paid $" << amount << " using credit card. Card Number: " << cardNumber << "\n";
    }
};

// 具体策略：数字钱包支付
class DigitalWalletPayment : public PaymentStrategy {
private:
    std::string walletID;

public:
    explicit DigitalWalletPayment(const std::string& walletId) : walletID(walletId) {}

    void pay(int amount) const override {
            std::cout << "Paid $" << amount << " using digital wallet. Wallet ID: " << walletID << "\n";
    }
};

// 上下文类
class PaymentContext {
private:
    std::shared_ptr<PaymentStrategy> strategy; // 当前支付策略

public:
    explicit PaymentContext(std::shared_ptr<PaymentStrategy> strat) : strategy(std::move(strat)) {}

    void setStrategy(const std::shared_ptr<PaymentStrategy>& strat) {
            strategy = strat;
    }

    void executePayment(int amount) const {
            strategy->pay(amount);
    }
};

// 客户端代码
int main() {
    // 使用现金支付
    auto cashPayment = std::make_shared<CashPayment>();
    PaymentContext context(cashPayment);
    context.executePayment(100);

    // 切换到信用卡支付
    auto creditCardPayment = std::make_shared<CreditCardPayment>("1234-5678-9876-5432");
    context.setStrategy(creditCardPayment);
    context.executePayment(200);

    // 切换到数字钱包支付
    auto digitalWalletPayment = std::make_shared<DigitalWalletPayment>("WALLET12345");
    context.setStrategy(digitalWalletPayment);
    context.executePayment(300);

    return 0;
}